package queue;

import java.util.Arrays;

public class Q573 {

	/**
	 * 任务调度器
	 * @param tasks
	 * @param n
	 * @return
	 */
	public static int leastInterval(char[] tasks, int n) {
		/**
		 * 1. 将所有的任务进行排序
		 * 2. 挑选最多次数的n+1个任务
		 */
		int[] map = new int[26];
		
		for(char c : tasks) {
			map[c-'A']++;
		}
		
		Arrays.sort(map);
		int time = 0;
		
		while(map[25] > 0) {
			//只要数组中还有大于零的数
			int m = 0;
			while(m <= n) {
				if(map[25] == 0)
					break;
				if(  m < 26 && map[25-m] > 0)
					map[25-m]--;
				time++;
				m++;
			}
			Arrays.sort(map);
		}
		return time;
	}
	
	public static void main(String[] args) {
		char[] cc = new char[5];
		cc[0] = 'A';
		cc[1] = 'B';
		cc[2] = 'B';
		cc[3] = 'B';
		cc[4] = 'A';
		int time = leastInterval(cc,2);
		System.out.println(time);
	}
	
	public static int leastInterval2(char[] tasks,int n) {
		
		
		
		
		return -1;
	}
}
